package com.common.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

public class Mail {
	Logger log = Logger.getLogger(this.getClass());

	private String from = "loveover_wj@163.com";// 发件邮箱地址

	private String fromName = "sendname";// 发件名称

	private String hostName = "smtp.163.com";// 设置发信的smtp服务器 smtp.163.com

	private String username = "loveover_wj";// 发件人用户名

	private String password = "wangjian";// 发件人用户密码

	public Mail(String from, String fromName, String hostName, String username,String password) {
		super();
		this.from = from;
		this.fromName = fromName;
		this.hostName = hostName;
		this.username = username;
		this.password = password;
	}

	public Mail() {
		super();
	}

	public void send(String to,/* 收信人地址 */
					String toName,// 收信人姓名
					String subject, /* 主题 */
					String body /* 内容 */
					) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setHostName(hostName);// 设置发信的smtp服务器
			email.setFrom(from, fromName);// 设置发信的邮件帐号和发信人
			email.setAuthentication(username, password);// 如果smtp服务器需要认证的话，在这里设置帐号、密码

			email.addTo(to, toName);// 设置收件人帐号和收件人
			email.setSubject(subject);// 设置邮件主题
			email.setCharset("utf-8");
			email.setHtmlMsg(body);// 设置邮件正文和字符编码

			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Mail mail = new Mail();
		mail.send("20367120@qq.com", "test1", "hello", "mail send test11");
	}

	public String getFrom() {
		return from;
	}

	public String getFromName() {
		return fromName;
	}

	public String getHostName() {
		return hostName;
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}